package com.huarui.saleOrder.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.huarui.purchaseOrder.model.PurOrSaleOrderDetail;
import com.huarui.saleOrder.dao.ISaleOrderDetailsDao;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class SaleOrderDetailsDaoImpl extends BaseDaoImpl implements ISaleOrderDetailsDao{
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> loadDetailsByOrdersId(Map<String, Object> paramsMap) {
		return (List<Map<String, Object>>) this.getSqlMapClientTemplate().queryForList(super.namespace + ".loadDetailsByOrdersId", paramsMap);
	}
	
	@Override
	public Integer batchInsert(final List<PurOrSaleOrderDetail> list) {
		return (Integer) this.getSqlMapClientTemplate().execute(
			new SqlMapClientCallback() {
				@Override
				public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
					executor.startBatch();
					
					for(PurOrSaleOrderDetail dt : list) {
						executor.insert(namespace + ".create", dt);
					}
					
					Integer num = executor.executeBatch();
					
					return num;
				}
			}
		);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Integer> getDetailsIdByOrderId(Integer saleOrderId) {
		return (List<Integer>) this.getSqlMapClientTemplate().queryForList(super.namespace + ".getDetailsIdByOrderId", saleOrderId);
	}
	
	@Override
	public Integer deleteBatch(List<Integer> ids) {
		return this.getSqlMapClientTemplate().delete(super.namespace + ".deleteDetailsByIds", ids);
	}
	
	@Override
	public Integer updateBatch(final List<PurOrSaleOrderDetail> list) {
		return (Integer) this.getSqlMapClientTemplate().execute(
			new SqlMapClientCallback() {
				@Override
				public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
					executor.startBatch();
					for(PurOrSaleOrderDetail d : list) {
						executor.update(namespace + ".update", d);
					}
					
					Integer num = executor.executeBatch();
					
					return num;
				}
			}
		);
	}
	
	@Override
	public BigDecimal getAmountByOrdersId(Integer saleOrderId) {
		return (BigDecimal) this.getSqlMapClientTemplate().queryForObject(super.namespace + ".getAmountByOrdersId", saleOrderId);
	}
	
	@Override
	public Integer deleteDetailsByOrdersId(Integer ordersId) {
		return this.getSqlMapClientTemplate().delete(super.namespace + ".deleteDetailsByOrdersId", ordersId);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> querySaleDetailsListByComplex(
			Map<String, Object> paramsMap) {
		return this.getSqlMapClientTemplate().queryForList(super.namespace + ".querySaleDetailsListByComplex", paramsMap);
	}

	@Override
	public Integer querySaleDetailsListCountByComplex(
			Map<String, Object> paramsMap) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(super.namespace + ".querySaleDetailsListCountByComplex", paramsMap);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getSaleOrderDetailByOrderId(Integer orderId) {
		return (List<Map<String, Object>>) getSqlMapClientTemplate().queryForList(namespace+".getSaleOrderDetailsByOrderId", orderId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void batchUpdateSaleOrderPickedQty(final List<Map> orderMap) {
		 this.getSqlMapClientTemplate().execute(
				new SqlMapClientCallback() {
					@Override
					public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
						executor.startBatch();
						
						for(Map map : orderMap) {
							executor.update(namespace + ".UpdateSaleOrderPickedQty", map);
						}
						
					    executor.executeBatch();
						
						return null;
					}
				}
			);
	}
}
